package bch.netty.handler.outbound;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelOutboundHandlerAdapter;
import io.netty.channel.ChannelPromise;
import io.netty.handler.codec.http.websocketx.TextWebSocketFrame;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class OutBoundHandlerB extends ChannelOutboundHandlerAdapter {
    @Override
    public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
        if (msg instanceof TextWebSocketFrame frame) {
            // 修改文本内容
            log.info("【OutBoundHandlerB】 decorate message {}" ,frame.text());
            TextWebSocketFrame newFrame = new TextWebSocketFrame("【OutBoundHandlerB】 " + frame.text());
            super.write(ctx, newFrame, promise);
            // 注意释放原 frame
            frame.release();
        } else {
            super.write(ctx, msg, promise);
        }
    }
}
